import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

//createRouter创建路由实例
//配置 history 模式: createWebHistory  地址栏不带#
//配置 hash 模式:     createHashHistory 地址栏带#
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path:'/login',component:()=>import('../views/login/LoginPage.vue')}, //登入页
     {
      path:'/',component:()=>import('../views/layout/LayoutContainer.vue'), //一旦访问了这个路径,需要重定向第一个文章组件 文章管理
      redirect:'/article/manage', //接下来配置他的二级路由
      children :[
        {path:'/article/manage',component:()=>import('../views/article/ArticleManage.vue')},
        {path:'/article/channel',component:()=>import('../views/article/ArticleChannel.vue')},
        {path:'/user/profile',component:()=>import('../views/user/UserProfile.vue')},
        {path:'/user/avatar',component:()=>import('../views/user/UserAvatar.vue')},
        {path:'/user/password',component:()=>import('../views/user/UserPassword.vue')},
      ]

     },
  ]
})

//登入访问拦截,默认不写 return 直接放行
router.beforeEach((to)=>{
  // 如果没有 token,且访问的是非登入界面,拦截到登录,其他情况正常放行
  const useStore=useUserStore()
  if(!useStore.token&&to.path!=='/login') return '/login'
})

export default router
